package com.shangxian.giveithome.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateTime {


    public static final String YMD = "yyyy-MM-dd";

    public static Date getTime(String type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String format = simpleDateFormat.format(new Date());
        try {
            return simpleDateFormat.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Integer getDays(Date d1) {
        Date date = getTime(YMD);
        return (int) ((date.getTime() - d1.getTime()) / (1000 * 3600 * 24));
    }

    //计算时间差，以天数为单位。如：2018-08-08 和 2018-08-05 相差3天
    public static int getDistanceTime(Date startTime, Date endTime) {
        int days = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        days = (int) (diff / (24 * 60 * 60 * 1000));
        return days;
    }

    //计算时间差，以小时为单位。如：2018-08-08 和 2018-08-07 相差24h
    public static double getDistanceTime2(Date startTime, Date endTime) {
        double hour = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        hour = (diff / (60 * 60 * 1000));
        return hour;
    }

}
